package com.hutou.chuangke.widget.banner;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;


import com.hutou.chuangke.R;

import static android.graphics.Paint.ANTI_ALIAS_FLAG;


/**
 * TODO: document your custom view class.
 */
public class BannerIndicatorView extends View implements ViewPager.OnPageChangeListener {
    public static final String EXCEPTION_VIEWPAGER_NO_NULL = "view pager can not be null";
    public static final String EXCEPTION_VIEWPAGER_ADAPTER_WRONG = "view pager adapter should be BannerPagerAdapter";

    private final Paint mPaintNormal = new Paint(ANTI_ALIAS_FLAG);
    private final Paint mPaintSelected = new Paint(ANTI_ALIAS_FLAG);

    private float mRadius;
    private float mCircleMargin;

    private ViewPager mViewPager;

    public BannerIndicatorView(Context context) {
        super(context);
    }

    public BannerIndicatorView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BannerIndicatorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        final Resources res = getResources();
        final int defaultNormalColor = res.getColor(R.color.light_gray); // 圆点填充色
        final int defaultSelectedColor = res.getColor(R.color.gray); // 默认圆点线的颜色
        final float defaultRadius = 8;
        final float defaultCircleMargin = 12;

        mPaintNormal.setAntiAlias(true);
        mPaintNormal.setColor(defaultNormalColor);
        mPaintSelected.setAntiAlias(true);
        mPaintSelected.setColor(defaultSelectedColor);
        mRadius = defaultRadius;
        mCircleMargin = defaultCircleMargin;
    }

    /**
     * get really position
     *
     * @param position
     * @return
     */
    private int getPosition(int position) {
        BannerPagerAdapter adapter = (BannerPagerAdapter) mViewPager.getAdapter();
        final int count = adapter.getActualCount();
        return position % count;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mViewPager == null) {
            return;
        }

        BannerPagerAdapter adapter = (BannerPagerAdapter) mViewPager.getAdapter();
        final int count = adapter.getActualCount();
        final int currentPosition = getPosition(mViewPager.getCurrentItem());
        float xCurrent = 0;
        for (int i = 0; i < count; i++) {
            if (i == currentPosition) {
                canvas.drawCircle(xCurrent + mRadius + getPaddingLeft(), mRadius + getPaddingTop(), mRadius, mPaintSelected);
            } else {
                canvas.drawCircle(xCurrent + mRadius + getPaddingLeft(), mRadius + getPaddingTop(), mRadius, mPaintNormal);
            }
            xCurrent += 2 * mRadius + mCircleMargin;
        }
    }

    public void setViewPager(ViewPager viewPager) {
        if (viewPager == null) {
            throw new IllegalArgumentException(EXCEPTION_VIEWPAGER_NO_NULL);
        }
        if (!(viewPager.getAdapter() instanceof BannerPagerAdapter)) {
            throw new RuntimeException(EXCEPTION_VIEWPAGER_ADAPTER_WRONG);
        }

        mViewPager = viewPager;
        mViewPager.setOnPageChangeListener(this);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measureLong(widthMeasureSpec), measureShort(heightMeasureSpec));
    }

    private int measureLong(int widthMeasureSpec) {
        int result;
        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        int specSize = MeasureSpec.getSize(widthMeasureSpec);

        if ((specMode == MeasureSpec.EXACTLY) || mViewPager == null) {
            result = specSize;
        } else {
            BannerPagerAdapter adapter = (BannerPagerAdapter) mViewPager.getAdapter();
            final int count = adapter.getActualCount();
            if (count == 0) {
                result = 0;
            } else {
                //+1是让 int 向上取整
                result = (int) (getPaddingLeft() + getPaddingRight() + 2 * mRadius * count + mCircleMargin * (count - 1) + 1);
            }
        }
        return result;
    }

    private int measureShort(int heightMeasureSpec) {
        int result;
        int specMode = MeasureSpec.getMode(heightMeasureSpec);
        int specSize = MeasureSpec.getSize(heightMeasureSpec);

        if ((specMode == MeasureSpec.EXACTLY) || mViewPager == null) {
            result = specSize;
        } else {
            BannerPagerAdapter adapter = (BannerPagerAdapter) mViewPager.getAdapter();
            if (adapter.getActualCount() == 0) {
                result = 0;
            } else {
                //+1是让 int 向上取整
                result = (int) (getPaddingTop() + getPaddingBottom() + 2 * mRadius + 1);
            }
        }
        return result;
    }

    @Override
    public void onPageScrolled(int i, float v, int i2) {

    }

    @Override
    public void onPageSelected(int i) {
        invalidate();
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
}
